The complete program is below. Notice how the pieces fit together.
Here is the complete program. Notice how the pieces fit together. The counting loop is nested in the body of the result-controlled loop.
class MillionDollarInterestRate
{
public static void main( String[] args )
{
double initialAmount = 1000.00 ;
double dollars = 0.0;
double rate;
int year;
rate = -0.001; // This is about to be increased to zero
while ( dollars < 1000000 )
{
// change to the next rate
rate = rate + 0.001;
// compute the dollars after 40 years at the current rate
year = 1 ;
dollars = initialAmount;
while ( year <= 40 )
{
dollars = dollars + dollars*rate ; // add another year's interest
dollars = dollars + 1000 ; // add in this year's contribution
year = year + 1 ;
}
}
System.out.println("After 40 years at " + rate*100
+ " percent interest you will have " + dollars + " dollars" );
}
}